package LearnDataStructure.b_散列结构.HashMap.官方HashMapAPI;

import java.util.*;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-05 14:14
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.b_散列结构.HashMap.官方HashMapAPI
 */
public class HashMapAPI {
    public static void main(String[] args) {
        Random random = new Random();
        HashMap<String, Integer> hashMap = new HashMap<>();

        hashMap.put("one",random.nextInt(10));
        hashMap.put("two",random.nextInt(10));
        hashMap.put("three",random.nextInt(10));
        hashMap.put("four",random.nextInt(10));
        hashMap.put("five",random.nextInt(10));

        System.out.println(hashMap);

        Set<Map.Entry<String, Integer>> entrySet = hashMap.entrySet();//获得键值对
        Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();//获得键值对的迭代器
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> element = iterator.next();
            System.out.println("键值对的key：" + element.getKey() + " , 键值对的value：" + element.getValue());
        }

        System.out.println("HashMap的size = " + hashMap.size());

        System.out.println("HashMap是否包含键 one ：" + hashMap.containsKey("one"));
        System.out.println("HashMap是否包含键 six ：" + hashMap.containsKey("six"));
        System.out.println("HashMap是否包含值 5 ：" + hashMap.containsValue(5));

        hashMap.remove("three");
        System.out.println(hashMap);

        System.out.println("HashMap 是否空：" + hashMap.isEmpty());

        hashMap.clear();
    }
}
